package com.example.impl;

import com.example.dao.CourseDao;
import com.example.entity.Course;
import com.example.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;

    @Override
    public Course getCourseById(Integer courseid) {
        return courseDao.findById(courseid);
    }

    @Override
    public List<Course> getAllCourses() {
        return courseDao.findAll();
    }

    @Override
    public boolean addCourse(Course course) {
        return courseDao.insert(course) > 0;
    }

    @Override
    public boolean updateCourse(Course course) {
        return courseDao.update(course) > 0;
    }

    @Override
    public boolean deleteCourse(Integer courseid) {
        return courseDao.delete(courseid) > 0;
    }

    @Override
    public List<Course> findCoursesByPage(int page, int pageNum) {
        return courseDao.findCoursesByStartIndexAndNum((page - 1) * pageNum, pageNum);
    }

    @Override
    public int totalPage(int pageNum) {
        int totalNum = courseDao.findCount();
        return (int) Math.ceil((double) totalNum / pageNum);
    }
}